import { axios } from "../../utils/request"
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    satrtTime: "",
    endTime: "",
    initData: [] as Room[],
  },
  //获取首页推荐房间数据
  async init() {
    let result = (await axios("/room/some", "GET", { key: 0 })) as Response
    const startRes = (await axios("/start")) as Response
    let initData = result.data.map((item) => {
      startRes.data.forEach((v: any) => {
        if (item.id == v.roomId) {
          item.isLike = true
        }
      })
      return item
    })
    this.setData({
      initData,
    })
  },
  async navChange(e: any) {
    wx.showLoading({
      title: "加载中",
      mask: true,
    })
    let key = +e.target.dataset.nav
    let result = (await axios("/room/some", "GET", { key })) as Response
    this.setData({
      active: e.target.dataset.nav,
      initData: result.data,
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 200)
  },
  selectDate() {
    wx.navigateTo({
      url: "/pages/date/date",
    })
  },
  toRooms() {
    wx.navigateTo({
      url: "/pages/rooms/rooms",
    })
  },
  //房间详情
  toDetail(e: any) {
    let item = e.currentTarget.dataset.index
    wx.navigateTo({
      url: "/pages/roomDetail/roomDetail",
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit("context", this.data.initData[item])
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    wx.showLoading({
      title: "加载中",
      mask: true,
    })
    this.getTabBar().setData({
      active: 0,
    })
    //格式化日期时间 显示开始时间 和结束时间
    let { start, end } = app.globData
    let startTime = start.format("M月D日")
    let endTime = end.format("M月D日")
    //获取初始化数据
    this.init()
    this.setData({
      startTime,
      endTime,
    })
    wx.hideLoading()
  },
})
